﻿function initFilter() {

    $("#PageSizeFilter").live("change", function () {
        ShowList();
    });

    $("#SearchButton").live("click", function () {
        ShowList();
    });

    $("#PublicPriceFromFilter, #PublicPriceToFilter").live("change", function () {
        if (this.value.indexOf(",") > -1) {
            this.value = this.value.replace(",", ".");
            $(this).trigger('change');
            return false;
        }
        try {
            var newValue = Math.round((this.value * 100)) / 100;
            if (this.value != newValue) {
                this.value = newValue;
                $(this).trigger('change');
                return false;
            }

        }
        catch (err) {
            this.value = "";
        }
    });

    $("#StockFromFilter, #StockToFilter").live("change", function () {
        if ($(this).val() != "")
            $(this).val(parseInt($(this).val()) ? parseInt($(this).val()) : "");
    });

    $("#PublicPriceFromFilter, #PublicPriceToFilter").live("change", function () {
        if ($(this).val() != "")
            $(this).val(parseFloat($(this).val()) ? parseFloat($(this).val()) : "");
    });
}

function ShowList() {
    $("#List").mask(loadingMessage + "...");
    var ascDirection;
    if (lastSortDirection == 'asc')
        ascDirection = true;
    else
        ascDirection = false;

    $.ajax(
        {
            url: $Url.resolve("~/Replacement/List"),
            data: { sortColumn: lastSortColumn, sortAscending: ascDirection, pageSize: $("#PageSizeFilter").val(), ShelfLocation: $("#ShelfLocationFilter").val(),
                RowLocation: $("#RowLocationFilter").val(), ColumnLocation: $("#ColumnLocationFilter").val(), Name: $("#NameFilter").val(), Code: $("#CodeFilter").val(),
                ReplacementBrandID: $("#ReplacementBrandFilter").val(), StockFrom: parseInt($("#StockFromFilter").val()), StockTo: parseInt($("#StockToFilter").val()),
                MinimumStock: parseInt($("#MinimumStockFilter").val()), PublicPriceFrom: parseFloat($("#PublicPriceFromFilter").val()), PublicPriceTo: parseFloat($("#PublicPriceToFilter").val())
            },
            type: "GET",
            cache: false,
            success: function (result) {
                $("#List").unmask();
                $("#List").html(result);
            },
            complete: function (xhr, status, err) {
                if (status == "error") {
                    $("#List").unmask();
                    handleGlobalError(xhr, status, err);
                }
            }
        });
}
